<template>
  <a-modal
    :open="open"
    width="640px"
    :title="rowData?.id ? '修改护理登记' : '新增护理登记'"
    :ok-button-props="{ disabled: !isCompleted }"
    centered
    :keyboard="false"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" :colon="false" :label-col="{ style: { width: '110px' } }">
      <a-form-item label="护理等级名称" class="w-1/2" v-bind="validateInfos.nurseLevelName">
        <a-input v-model:value="formState.nurseLevelName" placeholder="请输入" @change="getPinyin" />
      </a-form-item>
      <a-form-item label="助记码" class="w-1/2" v-bind="validateInfos.zjm">
        <a-input v-model:value="formState.zjm" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="等级图标" class="w-1/2" v-bind="validateInfos.iconCode">
        <a-select v-model:value="formState.iconCode" placeholder="请选择">
          <a-select-option v-for="(color, item) in nursingGrade" :key="item" :value="item">
            <span class="rounded p-1 w-6 text-white" :style="{ background: color }">{{ item }}</span>
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="顺序号" class="w-1/2" v-bind="validateInfos.outOrder">
        <a-input v-model:value.number="formState.outOrder" v-input:int placeholder="请输入" />
      </a-form-item>
      <a-form-item label="关联项目" class="w-full" v-bind="validateInfos.sfxmbName">
        <SelectTable v-model:value="formState.sfxmbName" :table-width="483" :search-params="{ xmlb: ['护理费'] }" type="关联项目" @select="onSelectProject" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" placeholder="请输入" :auto-size="{ minRows: 5 }" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { debounce } from 'lodash-es'
import { post } from '@/utils/request'
import { nursingGrade } from '@/utils/enums'

const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:value', 'confirm', 'success'])
const formState = ref({
  nurseLevelName: '',
  zjm: '',
  iconCode: null,
  outOrder: '',
  sfxmbName: ''
})
const rulesState = ref({
  nurseLevelName: [{ required: true, message: '请输入护理等级名称' }],
  iconCode: [{ required: true, message: '请选择等级图标' }],
  outOrder: [{ required: true, message: '请输入顺序号' }],
  zjm: [{ required: true, message: '请输入助记码' }],
  sfxmbName: [{ required: true, message: '请选择关联项目' }]
})
const { isCompleted, validateInfos } = useForm(formState, rulesState)

const onSelectProject = (row) => {
  formState.value.sfxmbBh = row.bh
  formState.value.sfxmbName = row.mc
  formState.value.cwf = row.dj ? row.dj.toFixed(2) : ''
}

const onOk = async () => {
  if (formState.value.outOrder > 99) return message.error('顺序号最大值不能超过99')
  const res = await post('/operation/opex/doSaveNursingGradeInfo', formState.value)
  res.code !== 0 ? message.error(res.msg || '操作失败') : message.success('保存成功')
  open.value = false
  emit('success')
}
const onCancel = () => {
  emit('confirm')
}
const getPinyin = debounce(async () => {
  const { data, code } = await post('/common/util/getPinyin', { name: formState.value.nurseLevelName })
  // code === 0 && emit('update:value', data)
  code === 0 && (formState.value.zjm = data)
}, 300)
watch(
  () => props.open,
  async () => {
    // await queryGradeList()
    formState.value = { ...props.rowData }
  }
)
</script>
<style lang="less" scoped>
.ant-form {
  display: flex;
  flex-flow: row wrap;
  min-width: 0;
  :deep(.ant-form-item) {
    .ant-form-item-label {
      width: 100px;
    }
  }
  .ant-picker {
    width: 100%;
  }
}
</style>
